mui.init();

Vue.use(VueLazyload, {
	preLoad: 1.3,
	error: '../../images/noimg.jpg',
	loading: '../../images/noimg.jpg'
});
var home = new Vue({
	el: '#home',
	data: {
		imgCommonUrl: common.imageUrl,
		data: '',
		//轮播图
		sliders: [],
		slidershead: '',
		slidersfoot: '',
		//楼层数据
		floors: '',
		//导航栏
		nav: [],
		//公告
		pub: [],
		//广告
		adverts: [],
		live: [],
		//专题显示
		special: [],
		scroll: ''
	},
	mounted: function() {
		//请求轮播图
		common.ajax('/index/getData', {

		}, function(data) {

			//			console.log(data.data)
			if(data.code == 1) {
				//轮播图数据
				home.sliders = data.data.result.sliders
				home.slidershead = home.sliders[0]
				home.slidersfoot = home.sliders[0]
				//模板7的数据格式

				for(var i = 0; i < data.data.result.floors.length; i++) {
					var template = {}
					template.main = []
					var num = -1
					if(data.data.result.floors[i].template == 7) {
						for(var j = 0; j < data.data.result.floors[i].adverts.length; j++) {
							if(j == 0) {
								template.head = data.data.result.floors[i].adverts[j]
							} else if(j == data.data.result.floors[i].adverts.length - 1) {
								template.footer = data.data.result.floors[i].adverts[j]
							} else {
								num++
								if(!template.main[Math.floor(num / 2)]) {
									template.main.push([data.data.result.floors[i].adverts[j]])
								} else {
									template.main[Math.floor(num / 2)].push(data.data.result.floors[i].adverts[j])
								}

							}
						}
						data.data.result.floors[i].templateList = template
					} else if(data.data.result.floors[i].template == 6) {
						data.data.result.floors[i].check = 0
						data.data.result.floors[i].showBanners = data.data.result.floors[i].banners[data.data.result.floors[i].check]
					}

				}
				home.floors = data.data.result.floors
				home.adverts = data.data.result.adverts
				home.data = data.data
				home.live = data.data.likes
				home.pub = data.data.pub
				home.nav = data.data.headMenuList
				home.$nextTick(function() {
					//轮播图
					var slider = mui("#slider");
					slider.slider({
						interval: 5000
					});
					setTimeout(function() {
						home.slidersfoot = home.sliders[home.sliders.length - 1]
					}, 5000)
					//公告轮播
					setTimeout(notice, 4000)

					mui('.mui-scroll-wrapper').scroll();
					$(".advert_three_img").height($(".advert_three_img").width())
					//尾部布局
					$(".guess_img").height($(".guess_img").width())
					$(".guess_img").eq(0).height($(".guess_img").width() - 32)
					home.scroll = mui('.muiScrollx').scroll({
						deceleration: 0.0005, //flick 减速系数，
					});
				})
			} else {
				if(data.resultMsg) {
					mui.toast(data.resultMsg)
				}
			}

		}, true, 'get')
	},
	methods: {
		shopcar: function() {
			var list = plus.webview.getWebviewById("ylindex.html")
			mui.fire(list, 'shopping');
		},
		//轮播图
		openSlider: function(data) {
			if(data.actionParam.searchParam.cates) {
				common.open('../seach/seach.html', '../seach/seach.html', data.actionParam.searchParam.cates.catName)
			} else if(data.actionParam.searchParam.goods) {
				common.open('../details/details.html', '../details/details.html', data.actionParam.searchParam.goods.goodsInfoId)
			} else if(data.actionParam.searchParam.themes) {
				common.open('../special/specialdetails.html', '../special/specialdetails.html', data.actionParam.searchParam.themes.themeId)
			} else if(data.actionParam.searchParam.urls) {
				mui.openWindow({
					id: "../mask/nesting.html",
					url: '../mask/nesting.html',
					extras: {
						url: data.actionParam.searchParam.urls.url
					}
				});
			}
		},
		//导航
		openNav: function(data) {
			if(data.name.indexOf('领券') >= 0 || data.name.indexOf('积分') >= 0) {
				common.open('../my/integral.html', '../my/integral.html')

			} else {

				if(data.categoryKeywords) {
					common.open('../seach/seach.html', '../seach/seach.html', data.categoryKeywords)
				}
			}
		},
		//火爆商品
		openDetails: function(data) {
			if(!data.actionParam.searchParam.goods.goodsInfoId) {
				return
			}
			common.open('../details/details.html', '../details/details.html', data.actionParam.searchParam.goods.goodsInfoId)
		},
		liveOpenDetails: function(data) {
			if(!data.prodId) {
				return
			}
			common.open('../details/details.html', '../details/details.html', data.prodId)
		},
		//专题上下张
		next: function(data) {
			data.check++
				data.showBanners = data.banners[data.check]
		},
		prev: function(data) {
			data.check--
				data.showBanners = data.banners[data.check]
		},
		//陈列查看更多
		seeData: function(data) {
			common.open('../seach/seach.html', '../seach/seach.html', data)
		},
		seespecial: function() {
			var list = plus.webview.getWebviewById("ylindex.html")
			mui.fire(list, 'special');
		},
		newShop: function(data) {
			var temp = ''
			if(data.banners) {
				temp = data.banners[0].img
			}
			common.open('hotShopping.html', 'hotShopping.html', temp)
		},
		//		openMain: function(data) {
		//			if(!data.actionParam.searchParam.urls || !data.actionParam.searchParam.urls.url) {
		//				return
		//			}
		//			mui.openWindow({
		//				id: "../mask/nesting.html",
		//				url: '../mask/nesting.html',
		//				extras: {
		//					url: data.actionParam.searchParam.urls.url
		//				}
		//			});
		//		},
		openSpecial: function(data) {
			if(data.actionParam.searchParam.themes.themeId) {
				common.open('../special/specialdetails.html', '../special/specialdetails.html', data.actionParam.searchParam.themes.themeId)
			}
		}
	}
})
//头部监听滚动渐变
document.getElementsByClassName('muiScrollx')[0].addEventListener("scroll", function(e) {
	var rgba = Math.abs(home.scroll.y) / 200
	if(Math.abs(home.scroll.y) <= 44) {
		$('.mui-bar-nav').css({
			'background': '-webkit-linear-gradient(#a0a0a0, transparent)',
			'background': '-o-linear-gradient(#a0a0a0, transparent)',
			'background': '-moz-linear-gradient(#a0a0a0, transparent)',
			'background': 'linear-gradient(#a0a0a0, transparent)'
		})
	} else {
		$('.mui-bar-nav').css('background', 'rgba(245, 44, 61,' + rgba + ')')
	}
});

mui.plusReady(function() {
	//	var _self = plus.webview.currentWebview();
	//	_self.setStyle({
	//		bounce: "vertical",
	//		bounceBackground: "red",
	//	});
	$('.seach_input input').on("tap", function() {
		var wp = plus.webview.create('../seach/seach.html', '../seach/seach.html', {
			scrollIndicator: 'none',
			scalable: false,
			popGesture: 'none'
		}, {
			preate: true
		})
		wp.show('fade-in');
	})
})

//公告
function notice(nums) {
	if(!$(".middle>div").hasClass('transition')) {
		$(".middle>div").addClass('transition')
	}
	var top = parseInt($(".middle div").css('top'))
	if($(".middle>div").height() / 2 - 28 >= Math.abs(top)) {
		$(".middle>div").css('top', top - 28)
		if($(".middle>div").height() / 2 - 28 == Math.abs(top)) {
			setTimeout(function() {
				$(".middle>div").removeClass('transition')
				$(".middle>div").css('top', 0)
			}, 1000)
		}
	}
	setTimeout(notice, 4000)
}

var _toast = false
mui.back = function() {
	if(!_toast || !_toast.isVisible()) {
		_toast = mui.toast('再按一次返回键退出', {
			type: 'div'
		});
	} else {
		var main = plus.android.runtimeMainActivity();
		main.moveTaskToBack(false);
	}
}